package com.ttg.utils;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;

import java.io.*;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * Class ZipUtils
 * Description
 * Create 2016-02-25 01:01:19 
 * @author Ardy    
 */
public abstract class ZipUtils {

    /** 
     * Field bufferLen
     * Description 
     */
    private static final int bufferLen = 8192;

    /**
     * Constructs ZipUtils
     * Description
     */
    public ZipUtils() {}

    /**
     * Method doCompress 
     * Description 说明：
     *
     * @param srcData 说明：
     * @param zipFileName 说明：
     *
     * @return 返回值说明：
     *
     * @throws IOException 异常：
     */
    public static byte[] doCompress(byte[] srcData, String zipFileName) throws IOException {
        ZipArchiveOutputStream out = null;
        byte[]                 var6;

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);

            out = new ZipArchiveOutputStream(bout);

            ZipArchiveEntry entry = new ZipArchiveEntry(zipFileName);

            entry.setSize((long) srcData.length);
            out.putArchiveEntry(entry);
            IOUtils.write(srcData, out);
            out.closeArchiveEntry();
            out.finish();

            byte[] result = bout.toByteArray();

            var6 = result;
        } finally {
            IOUtils.closeQuietly(out);
        }

        return var6;
    }

    /**
     * Method doCompress 
     * Description 说明：
     *
     * @param srcFile 说明：
     * @param zipFileName 说明：
     *
     * @return 返回值说明：
     *
     * @throws IOException 异常：
     */
    public static byte[] doCompress(File srcFile, String zipFileName) throws IOException {
        if (( srcFile != null ) && srcFile.exists() &&!srcFile.isDirectory()) {
            ZipArchiveOutputStream out = null;
            BufferedInputStream    is  = null;
            byte[]                 var7;

            try {
                is = new BufferedInputStream(new FileInputStream(srcFile), 8192);

                ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);

                out = new ZipArchiveOutputStream(bout);

                ZipArchiveEntry entry = new ZipArchiveEntry(zipFileName);

                entry.setSize(srcFile.length());
                out.putArchiveEntry(entry);
                IOUtils.copy(is, out);
                out.closeArchiveEntry();
                out.finish();

                byte[] result = bout.toByteArray();

                var7 = result;
            } finally {
                IOUtils.closeQuietly(is);
                IOUtils.closeQuietly(out);
            }

            return var7;
        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath()
                                               + "] must exist and cannot be a directory.");
        }
    }

    /**
     * Method doCompress 
     * Description 说明：
     *
     * @param srcFile 说明：
     * @param destFile 说明：
     *
     * @throws IOException 异常：
     */
    public static void doCompress(File srcFile, File destFile) throws IOException {
        if (( srcFile != null ) && srcFile.exists() &&!srcFile.isDirectory()) {
            if (( destFile != null ) && ( !destFile.exists() ||!destFile.isDirectory() )) {
                ZipArchiveOutputStream out = null;
                BufferedInputStream    is  = null;

                try {
                    is = new BufferedInputStream(new FileInputStream(srcFile), 8192);

                    if (!destFile.exists()) {
                        destFile.createNewFile();
                    }

                    out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), 8192));

                    ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());

                    entry.setSize(srcFile.length());
                    out.putArchiveEntry(entry);
                    IOUtils.copy(is, out);
                    out.closeArchiveEntry();
                    out.finish();
                } finally {
                    IOUtils.closeQuietly(is);
                    IOUtils.closeQuietly(out);
                }
            } else {
                throw new IllegalArgumentException(( "destFile[" + destFile == null )
                                                   ? "null"
                                                   : destFile.getAbsolutePath()
                                                       + "] cannot be empty and cannot be a directory.");
            }
        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath()
                                               + "] must exist and cannot be a directory.");
        }
    }

    /**
     * Method doDecompressToFile 
     * Description 说明：
     *
     * @param src 说明：
     * @param destFile 说明：
     *
     * @throws IOException 异常：
     */
    public static void doDecompressToFile(InputStream src, File destFile) throws IOException {
        ZipArchiveInputStream is = null;

        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(src, 8192));

            Object entry = null;

            while (is.getNextZipEntry() != null) {
                BufferedOutputStream os = null;

                try {
                    os = new BufferedOutputStream(new FileOutputStream(destFile), 8192);
                    IOUtils.copy(is, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    /**
     * Method doDecompress 
     * Description 说明：
     *
     * @param srcFile 说明：
     * @param destDir 说明：
     *
     * @throws IOException 异常：
     */
    public static void doDecompress(File srcFile, File destDir) throws IOException {
        if (( srcFile != null ) && srcFile.exists() &&!srcFile.isDirectory()) {
            ZipArchiveInputStream is = null;

            try {
                is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), 8192));

                ZipArchiveEntry entry = null;

                while (( entry = is.getNextZipEntry() ) != null) {
                    if (entry.isDirectory()) {
                        File os = new File(destDir, entry.getName());

                        os.mkdirs();
                    } else {
                        BufferedOutputStream os1 = null;

                        try {
                            os1 = new BufferedOutputStream(new FileOutputStream(new File(destDir,
                                    entry.getName())), 8192);
                            IOUtils.copy(is, os1);
                        } finally {
                            IOUtils.closeQuietly(os1);
                        }
                    }
                }
            } finally {
                IOUtils.closeQuietly(is);
            }
        } else {
            throw new IllegalArgumentException("srcFile[" + srcFile.getAbsolutePath()
                                               + "] must exist and cannot be a directory.");
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
